package online.hashmall.api;

/**
 * REST API 错误码
 *
 * @author jpq
 */
public enum ApiErrorCode implements IErrorCode {

    UNKNOWN_ERROR(500, "未知错误"),

    ILLEGAL_ARGUMENT(50100, "参数异常"),
    MISSING_SERVLET_REQUEST_PARAMETER(50101, "请求参数缺失"),
    NO_HANDLER_FOUND(20102, "找不到方法"),


    ILLEGAL_DATABASE_DATA(50200, "数据异常"),
    DUPLICATE_KEY_EXIST(50201, "值已存在"),


    FILE_UPLOAD_GET_BYTES_ERROR(50300, "文件上传错误"),
    FILE_UPLOAD_ERROR(50301, "文件上传错误"),

    PAGE_NOT_FOUND(50400, "页面不存在"),

    /**
     * 失败
     */
    FAILED(-1, "操作失败"),
    /**
     * 成功
     */
    SUCCESS(0, "执行成功");

    private final long code;
    private final String msg;

    ApiErrorCode(final long code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ApiErrorCode fromCode(long code) {
        ApiErrorCode[] ecs = ApiErrorCode.values();
        for (ApiErrorCode ec : ecs) {
            if (ec.getCode() == code) {
                return ec;
            }
        }
        return SUCCESS;
    }


    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
    }

    public long getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}
